package org.jeecg.modules.rooms.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.EnumUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.business.entity.BusAdvertContent;
import org.jeecg.modules.business.service.IBusAdvertContentService;
import org.jeecg.modules.rooms.DTO.CesRoomLayoutDto;
import org.jeecg.modules.rooms.DTO.CesRoomLayoutRemoveDto;
import org.jeecg.modules.rooms.DTO.CesRoomLayoutSearchDto;
import org.jeecg.modules.rooms.Enum.CouponEnums;
import org.jeecg.modules.rooms.entity.CesRoomLayout;
import org.jeecg.modules.rooms.service.CesRoomLayoutServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 租户房型记录表 前端控制器
 * </p>
 *
 * @author fendo
 * @since 2023-03-01
 */
@RestController
@RequestMapping("/rooms/cesRoomLayout")
@Api(tags="ces_room_layout")
@Slf4j
public class CesRoomLayoutController extends JeecgController<CesRoomLayout, CesRoomLayoutServiceImpl> {

    @Resource
    private CesRoomLayoutServiceImpl cesRoomLayoutService;

    @ApiOperation(value="房型分页列表查询", notes="房型分页列表查询")
    @GetMapping(value = "/list")
    public Result list(CesRoomLayoutSearchDto dto){
        if(dto.getPageNo().intValue() <= 0){
            return Result.error("页数不能小于等于0");
        }
        if(dto.getPageSize().intValue() < 10){
            return Result.error("分页条数不能小于10");
        }
        if(dto.getAppState() != null && dto.getAppState().intValue() != 0){
            List<Object> codes = EnumUtil.getFieldValues(CouponEnums.RoomLayoutAppStateEnum.class, "code");
            if(!codes.contains(dto.getAppState().intValue())){
                return Result.error("参数不正确!请选择正确的上架状态!");
            }
        }
        try{
            return  cesRoomLayoutService.findRoomLayoutList(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "创建房型房型")
    @ApiOperation(value="创建房型房型", notes="创建房型查询")
    @PostMapping(value = "/save")
    public Result createRoomLayout(@RequestBody CesRoomLayoutDto dto){
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(BigDecimal.ZERO.compareTo(dto.getMarketPrice()) == 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getCanLivePersonNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getBreakfastNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getLunchNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getDinnerNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return  cesRoomLayoutService.createRoomLayout(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "修改房型房型")
    @ApiOperation(value="修改房型房型", notes="修改房型查询")
    @PutMapping(value = "/modify")
    public Result modifyCesRoomLayout(@RequestBody CesRoomLayoutDto dto){
        if(StringUtils.isBlank(dto.getId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(BigDecimal.ZERO.compareTo(dto.getMarketPrice()) == 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getCanLivePersonNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getBreakfastNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getLunchNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getDinnerNum().intValue() < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return  cesRoomLayoutService.modifyCesRoomLayout(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @AutoLog(value = "删除房型房型")
    @ApiOperation(value="删除房型房型", notes="删除房型")
    @DeleteMapping(value = "/remove")
    public Result removeRoomLayout(CesRoomLayoutRemoveDto dto){
        if(CollectionUtil.isEmpty(dto.getRoomLayoutIds())){
            if(dto.getId() != null) {
                List<String> ids = new ArrayList<>();
                ids.add(dto.getId());
                dto.setRoomLayoutIds(ids);
            } else {
                return Result.error(ResultCode.PARAM_MISS);
            }
        }
        try{
            return  cesRoomLayoutService.removeRoomLayout(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "修改启用状态")
    @ApiOperation(value="修改启用状态", notes="修改启用状态")
    @PutMapping(value = "/modifyState/{roomLayoutId}")
    public Result modifyState(@PathVariable String roomLayoutId, @RequestParam String hotelId, @RequestParam Integer state){
        if(StringUtils.isBlank(roomLayoutId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(state < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return  cesRoomLayoutService.modifyState(roomLayoutId,hotelId, state.intValue() == 1 ? Boolean.TRUE: Boolean.FALSE);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "修改上架状态")
    @ApiOperation(value="修改上架状态", notes="修改上架状态")
    @PutMapping(value = "/modifyAppState/{roomLayoutId}")
    public Result modifyAppState(@PathVariable String roomLayoutId, @RequestParam String hotelId, @RequestParam Integer state){
        if(StringUtils.isBlank(roomLayoutId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(state < 0){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return  cesRoomLayoutService.modifyAppState(roomLayoutId,hotelId, state.intValue() == 1 ? Boolean.TRUE: Boolean.FALSE);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }














}

